// CommandLineHelper Library
// =========================
// License: Microsoft Permissive License (Ms-PL) v1.1
// Author: Rhy Mednick (http://www.rhyverse.net)
// Project Site: http://www.codeplex.com/CommandLineHelper
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using CommandLineHelper.Attributes;

namespace CommandLineHelper.Tests
{
    [TestFixture]
    public class ValidateLengthAttribute_Test
    {
        /// <summary>
        ///A test for IsValid (object)
        ///</summary>
        [Test]
        public void IsValid_ValidateLengthAttribute()
        {
            ValidateLengthAttribute target = new ValidateLengthAttribute();
            target.MaxLength = 10;
            target.MinLength = 0;

            //validate simple match at boundary
            object value = "HelloWorld";
            bool expected = true;
            bool actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateLengthAttribute.IsValid did not return the expected value.");

            //validate empty string (other boundary)
            value = String.Empty;
            expected = true;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateLengthAttribute.IsValid did not return the expected value.");

            //validate no match when outside boundary
            value = "Hello World";
            expected = false;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateLengthAttribute.IsValid did not return the expected value.");

        }

        /// <summary>
        ///Verify that the MinLength and MaxLength properties function correctly
        ///</summary>
        [Test]
        public void MinMaxLengthTest()
        {
            ValidateLengthAttribute target = new ValidateLengthAttribute();

            //Min and Max length cannot be less than zero
            int minVal = -100; //not valid
            try
            {
                target.MinLength = minVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(minVal, target.MinLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MinLength was not set correctly.");
            }
            int maxVal = -20; //not valid
            try
            {
                target.MaxLength = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(maxVal, target.MaxLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MaxLength was not set correctly.");
            }

            //max cannot be zero
            maxVal = 0; // not valid
            try
            {
                target.MaxLength = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(maxVal, target.MaxLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MaxLength was not set correctly.");
            }

            //reset
            target = new ValidateLengthAttribute();
            //max cannot be less than min, set min first
            minVal = 100;
            maxVal = 5;
            try
            {
                target.MinLength = minVal;
                target.MaxLength = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(minVal, target.MinLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MinLength was not set correctly.");
                Assert.AreNotEqual(maxVal, target.MaxLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MaxLength was not set correctly.");
            }

            //reset
            target = new ValidateLengthAttribute();
            //max cannot be less than min, set max first
            try
            {
                target.MaxLength = maxVal;
                target.MinLength = minVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(minVal, target.MinLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MinLength was not set correctly.");
                Assert.AreEqual(maxVal, target.MaxLength, "CommandLineHelper.Attributes.ValidateLengthAttribute.MaxLength was not set correctly.");
            }
        }
    }
}
